

import UIKit

enum DateFormat: String {
    // 年-月-日 时-分-秒
    case yearMonthDayHourMinuteSecond       = "yyyy-MM-dd HH:mm:ss"
    // 年-月-日 时-分
    case yearMonthDayHourMinute             = "yyyy-MM-dd HH:mm"
    // 年-月-日 时
    case yearMonthDayHour                   = "yyyy-MM-dd HH"
    // 年-月-日
    case yearMonthDay                       = "yyyy-MM-dd"
    // 年-月-日
    case yearMonth                          = "yyyy-MM"
    // 年
    case year                               = "yyyy"
    
    // 月-日 时-分-秒
    case MonthDayHourMinuteSecond           = "MM-dd HH:mm:ss"
    // 月-日 时-分
    case MonthDayHourMinute                 = "MM-dd HH:mm"
    // 月-日 时
    case MonthDayHour                       = "MM-dd HH"
    // 月-日
    case MonthDay                           = "MM-dd"
    // 月
    case Month                              = "MM"
    
    // 日
    case Day                                = "dd"
    
    // 时-分-秒
    case HourMinuteSecond                   = "HH:mm:ss"
    // 时-分
    case HourMinute                         = "HH:mm"
    // 时
    case Hour                               = "HH"
    
}

//MARK: - Date
extension Date {
    
    //TODO: - 日期转字符串
    func toString(_ format: DateFormat = .yearMonthDay) -> String {
        toString(format.rawValue)
    }
    
    //TODO: - 日期转字符串
    func toString(_ format: String) -> String {
        let formatter = DateFormatter.init()
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
    
    //TODO: - Date 转 时间戳 (毫秒)
    func toTimeStamp() -> Int {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let timeStamp = CLongLong(round(timeInterval*1000))
        return Int(timeStamp)
    }
    
}

extension String {
    
    func toDate(_ format: String) -> Date? {
        let formatter = DateFormatter.init()
        formatter.dateFormat = format
        formatter.timeZone = .current
        return formatter.date(from: self)
    }
    
}


extension Int {
    
    //TODO - 时间戳转字符串
    func toTimeStr(_ format: DateFormat = .yearMonthDay) -> String {
        toTimeStr(format.rawValue)
    }
    
    //TODO - 时间戳转字符串
    func toTimeStr(_ format: String) -> String {
        /// 是否为毫秒
        let isMillisecond = "\(self)".count >= 13
        /// 毫秒就➗ 1000, 否则就不用
        let timeInterval = self / (isMillisecond ? 1000 : 1)
        let date:Date = Date.init(timeIntervalSince1970: TimeInterval(timeInterval))
        let formatter = DateFormatter.init()
        formatter.dateFormat = format
        return formatter.string(from: date as Date)
    }
    
    func toPlayTimeStr() -> String {
        let hour = self/3600
        let min = self/60
        let second = self - hour*3600 - min*60
        var res = String(format: "%02d:%02d:%02d", hour,min,second)
        if hour <= 0 {
            res = String(format: "%02d:%02d", min,second)
        }
        return res
    }
    
    // 转换成HH:mm:ss
    func toHHMMSSTimeStr() -> String {
        return String(format: "%02d:%02d:%02d", self / 3600, (self % 3600) / 60, (self % 3600) % 60)
    }
}
